<template>
  <d2-container v-loading.fullscreen.lock='loading'>

    <el-form >
      <el-row>
        <el-col :span="12">
          <div style="display: inline-block;">学生：</div>
          <el-select v-model="student" filterable placeholder="搜索学生姓名"  @change="studentChange">
            <el-option
            v-for="item in studentList"
            :key="item._id"
            :label="item.name + ' ' + (item._id == 'null' ? '' : (item.gender == 0 ? '男' : '女'))"
            :value="item._id"
            ></el-option>
          </el-select>
          <el-button @click="newStudent" type='primary' style='margin-left:50px;'>新建学生
          </el-button>
        </el-col>
      </el-row>
      <el-row :span="12" v-if='student != undefined' style='background-color: #F6FAFE;height:40px;margin-top:10px;line-height: 40px;padding-left:10px;position: relative;'>
        <div style="font-size:18px;display: inline-block;">{{studentList[studentIndex].name}}</div>
        <div v-if="student != 'null'" style="display: inline-block;">
          <div style="margin-left:3px;display: inline-block;font-size:15px">{{studentList[studentIndex].gender == 0 ? ' 男' : ' 女'}}</div>
          <div style="display: inline-block;margin-left: 10px;font-size:15px"> | <span style="margin-left:5px;background-color: red;display: inline-block;height:30px;width: 30px;text-align: center;position: relative;top:0px;line-height: 30px;color: rgb(96, 186, 178); border-color: rgb(96, 186, 178); background-color: rgb(221, 248, 239);border-style: solid;border-width: 1px;border-radius: 3px">课</span> 
          在读课程： {{courseCount}}</div>
          <div style="display: inline-block;margin-left: 10px;font-size:15px">| 
            <span style="margin-left:5px;background-color: red;display: inline-block;height:30px;width: 30px;text-align: center;position: relative;top:0px;line-height: 30px;color: rgb(153, 100, 245); border-color: rgb(188, 168, 255); background-color: rgba(188, 168, 255, 0.15);border-style: solid;border-width: 1px;border-radius: 3px">
              <!-- 校 -->
              班
            </span>
            <!-- 在读校区:  {{studentList[studentIndex].school.name}} -->
            在读班级：{{avClassText}}
          </div>
          <div style="display: inline-block;margin-left: 10px;font-size:15px">| 
            <span style="margin-left:5px;background-color: red;display: inline-block;height:30px;width: 30px;text-align: center;position: relative;top:0px;line-height: 30px;color: rgb(52, 178, 211); border-color: rgb(96, 186, 178); background-color: rgb(182, 238, 251);border-style: solid;border-width: 1px;border-radius: 3px">联</span>
          联系方式： {{parentInfo}}</div>
        </div>
      </el-row>

      <el-row :span="12" style="background-color: white;height:1px;margin-top: 10px"></el-row>

      <!-- <block name="courseList"> -->
        <el-row :span="6" style="margin-top:10px;height:40px"><span style='width:2px;height:100%;background-color:rgb(52, 178, 211) ;color: rgb(52, 178, 211);margin-right: 3px'>_</span>课程信息
          <el-button @click="showCourse = true" type='primary' style='height:30px;position: relative;top:0px;font-size: 14px;padding: 5px;margin-left:50px;' :disabled='disabled'>选择课程
          </el-button>
          <el-button @click="showClass = true" type='primary' style='margin-left:10px;height:30px;position: relative;top:0px;font-size: 14px;padding: 5px;' :disabled='disabled'>选择班级
          </el-button>

         <!--  <span style='margin-left:15px;color:#60BAB2' v-if='student != undefined'>
          在读班级：{{avClassText}}
        </span> -->

      </el-row>
      <el-row v-if="courseOrder.length == 0" style='background-color: white;height:300px'>
        <div style="color:#CACBCC;width: 100%;text-align: center;line-height: 300px;height: 300px">请点击左上方按钮选择报名信息</div>

      </el-row>

      <el-row style='background-color:white;width: 100%;height:300px;position: relative;border-width: 1px;border-color: #D4DFE5;border-style: solid;margin-bottom:20px' v-for='(order,index) in courseOrder'  >


        <div style="background-color:#F6FAFE;height:60px;width:100%;overflow: hidden;white-space: nowrap;">
          <div style="display: inline-block;font-size:15px;width: 300px"> <span style="margin-left:5px;background-color: red;display: inline-block;height:30px;width: 30px;text-align: center;position: relative;top:0px;line-height: 30px;color: rgb(96, 186, 178); border-color: rgb(96, 186, 178); background-color: rgb(221, 248, 239);border-style: solid;border-width: 1px;border-radius: 3px;margin-top:13px;margin-left: 15px">课</span> 
            {{courseOrder[index].course.name}}

          </div>
          <div style="display: inline-block;font-size:15px;width: 250px;">
            <span style="margin-left:5px;background-color: red;display: inline-block;height:30px;width: 30px;text-align: center;position: relative;top:0px;line-height: 30px;color: rgb(153, 100, 245); border-color: rgb(188, 168, 255); background-color: rgba(188, 168, 255, 0.15);border-style: solid;border-width: 1px;border-radius: 3px">校</span>
            {{courseOrder[index].course.school.name}}
            <!-- 总校区 -->
          </div>

          <div style="display: inline-block;font-size:15px;width: 300px">
           <span style="margin-left:5px;background-color: red;display: inline-block;height:30px;width: 30px;text-align: center;position: relative;top:0px;line-height: 30px;color: rgb(52, 178, 211); border-color: rgb(96, 186, 178); background-color: rgb(182, 238, 251);border-style: solid;border-width: 1px;border-radius: 3px">班</span>

           <el-select v-model="order.class" filterable placeholder="搜索班级名称"   @click.native='classClick(index)' style='margin-left:10px' @change='classChange(index)' s>
            <el-option
            v-for="item in order.courseOrderClassList"
            :key="item._id"
            :label="item.name + '  开班时间(' + item.startDateText + ')  授课老师(' + item.teacher + ')  剩余名额(' + (item.studentLimit - item.students) + ')' "
            :value="item._id"
            ></el-option>
          </el-select>
        </div>

        <div style="display: inline-block;position: absolute;right:5px">
          <button style="height:30px;width: 30px;background-color: #F6E4E7;border-style: solid;border-width: 1px;border-color: #FB6363;margin-top: 15px;border-radius: 3px" @click="removeCourseOrder(index)"><i class="el-icon-close" style="color: #FB6363;" ></i> 
          </button>
          <div style="display: inline-block;color:#98A0A7;position: absolute;right:265px;line-height: 60px;font-size: 14px">共：</div>

          <div style="display: inline-block;position: absolute;right:223px;line-height: 60px;font-size: 20px;font-weight: 500;text-align: center;width: 50px">
          {{courseOrder[index].amount}}</div>
          <div style="display: inline-block;color:#98A0A7;position: absolute;right:200px;line-height: 60px;font-size: 14px" v-if="courseOrder[index].type == 0">课时</div>

          <div style="display: inline-block;color:#98A0A7;position: absolute;right:200px;line-height: 60px;font-size: 14px" v-if="courseOrder[index].type == 1">小时</div>

          <div style="display: inline-block;color:#98A0A7;position: absolute;right:200px;line-height: 60px;font-size: 14px" v-if="courseOrder[index].type == 2">月</div>

          <div style="display: inline-block;color:#98A0A7;position: absolute;right:140px;line-height: 60px;font-size: 14px">小计：</div>
          <div style="display: inline-block;position: absolute;right:53px;line-height: 60px;font-size: 20px;font-weight: 500;text-align: center;width: 100px" >{{parseFloat(courseOrder[index].total - courseOrder[index].discount).toFixed(2)}}</div>
          <div style="display: inline-block;color:#98A0A7;position: absolute;right:45px;line-height: 60px;font-size: 14px" >元</div>
        </div>


      </div>

      <div style="width: 100%;position: relative;height: 1px;background-color: #D4DFE5">
      </div>

      <div style="width: 100%;position: relative;">
        <div style="position: relative;left:30px;top:20px;color:#98A0A7;display: inline-block;width: 100px">收费模式</div>
        <el-select v-model="order.type" filterable @change='orderTypeChange(index)' style='display: inline-block;margin-left:10px;position: relative;;left:10px;top:20px;width: 150px'
        >
        <el-option
        v-for="item in order.pays"
        :label="item.name"
        :value="item.type"
        ></el-option>
      </el-select>
      <div style="position: relative;left:20px;top:20px;display: inline-block;width: 100px"> <i class="el-icon-close"></i></div>
      <el-input style="position: relative;left:-55px;top:20px;display: inline-block;width: 100px" v-model='order.amount' @change="amountChange(index)" type="number" min="0">
      </el-input>
      <div style="position: relative;left:-40px;top:20px;display: inline-block;width: 100px"> = {{parseFloat(order.total).toFixed(2)}} 元</div>
    </div>

    <div style="width: 100%;position: relative;height: 1px;background-color: #D4DFE5;top:40px">
    </div>

    <div style="width: 100%;position: relative;top:40px">
      <div style="position: relative;left:30px;top:20px;color:#98A0A7;display: inline-block;width: 100px">课程优惠</div>

      <el-input style="position: relative;left:20px;top:20px;display: inline-block;width: 100px" v-model='order.discount' type="number" min="0" @change='orderDiscountChange(index)'>

      </el-input>
      <div style="position: relative;left:30px;top:20px;display: inline-block;width: 100px"> 元</div>

      <el-checkbox v-model="order.valid" style='display: inline-block;position: relative;top:20px;margin-left:15px;font-size: 18px'>有效期</el-checkbox>
      <el-date-picker
      v-model="order.validDate"
      type="date"
      placeholder="选择截止有效期"
      :disabled="!order.valid"
      style='position: relative;top:20px;left:10px'
      ></el-date-picker>

      <div style="display: inline-block;" v-if='order.type == 2'>
        <div style="position: relative;left:50px;top:20px;color:#98A0A7;display: inline-block;width: 100px" >开始月份</div>
        <el-date-picker
        v-model="order.startDate"
        type="month"
        placeholder="选择开始月份"
        @change="monthChange"
        style='position: relative;top:20px;left:40px'
        ></el-date-picker>
      </div>
    </div>


    <div style="width: 100%;position: relative;height: 1px;background-color: #D4DFE5;top:80px">
    </div>

    <div style="width: 100%;position: relative;top:40px">

    </div>

    <div style="position: relative;left:30px;top:100px;color:#98A0A7;display: inline-block;width: 100px">对内备注</div>

    <el-input style="position: relative;left:20px;top:100px;display: inline-block;width: 40%" v-model='order.memoIn' placeholder='内部人员可见'>

    </el-input>

    <div style="position: relative;left:30px;top:100px;color:#98A0A7;display: inline-block;width: 100px">对外备注</div>

    <el-input style="position: relative;left:20px;top:100px;display: inline-block;width: 40%" v-model='order.memoOut' placeholder='订单打印可见'>

    </el-input>

  </el-row>



  <!-- <block name="courseList"> -->
    <el-row :span="6" style="margin-top:10px;height:40px"><span style='width:2px;height:100%;background-color:rgb(52, 178, 211) ;color: rgb(52, 178, 211);margin-right: 3px' >_</span>教材信息
      <el-button @click="showProduct = true" type='primary' style='height:30px;position: relative;top:0px;font-size: 14px;padding: 5px;margin-left:50px;'>选择教材
      </el-button>
      <el-row v-if="productOrder.length == 0" style='background-color: white;height:300px;margin-top: 10px'>
        <div style="color:#CACBCC;width: 100%;text-align: center;line-height: 300px;height: 300px">请点击左上方按钮选择教材信息</div>
      </el-row>

      <el-row style='background-color:white;width: 100%;position: relative;border-width: 1px;border-color: #D4DFE5;border-style: solid;margin-bottom:20px;margin-top: 10px'  v-else>


        <div style="background-color:#F6FAFE;height:60px;width:100%;overflow: hidden;white-space: nowrap;">

          <div style="display: inline-block;position: absolute;right:5px">
            <button style="height:30px;width: 30px;background-color: #F6E4E7;border-style: solid;border-width: 1px;border-color: #FB6363;margin-top: 15px;border-radius: 3px" @click="removeProductOrder()"><i class="el-icon-close" style="color: #FB6363;" ></i> 
            </button>


            <div style="display: inline-block;color:#98A0A7;position: absolute;right:140px;line-height: 60px;font-size: 14px">小计：</div>
            <div style="display: inline-block;position: absolute;right:53px;line-height: 60px;font-size: 20px;font-weight: 500;text-align: left;width: 80px" >{{parseFloat(productOrderTotal - productOrderDiscount).toFixed(2)}}</div>
            <div style="display: inline-block;color:#98A0A7;position: absolute;right:40px;line-height: 60px;font-size: 14px" >元</div>
          </div>


        </div>

        <div style="width: 100%;position: relative;height: 1px;background-color: #D4DFE5">
        </div>

        <div style="width: 100%;position: relative;">
          <div v-for='(order,index) in productOrder' style="margin-bottom:10px">
            <el-checkbox v-model="order.picked" style='display: inline-block;position: relative;top:20px;margin-left:15px;font-size: 18px'>已领取</el-checkbox>
            <div style="position: relative;left:30px;top:20px;color:#98A0A7;display: inline-block;width: 100px">{{order.product.name}}</div>
            <div style="position: relative;left:20px;top:20px;display: inline-block;width: 100px"> {{parseFloat(order.product.sellingPrice).toFixed(2)}}</div>
            <div style="position: relative;left:0px;top:20px;display: inline-block;width: 100px"> <i class="el-icon-close"></i></div>
            <el-input style="position: relative;left:-55px;top:20px;display: inline-block;width: 100px" v-model='order.amount' @change="productAmountChange(index)" type="number" min="0">
            </el-input>
            <div style="position: relative;left:-20px;top:20px;display: inline-block;width: 100px"> = {{parseFloat(order.product.sellingPrice * order.amount).toFixed(2)}} 元</div>
            <button style="height:30px;width: 30px;background-color: #F6E4E7;border-style: solid;border-width: 1px;border-color: #FB6363;border-radius: 3px;position: relative;top:20px" @click="removeProductOrderDetail(index)"><i class="el-icon-close" style="color: #FB6363;" ></i> 
            </button>
          </div>
        </div>

        <div style="width: 100%;position: relative;height: 1px;background-color: #D4DFE5;top:40px">
        </div>

        <div style="width: 100%;position: relative;top:20px;height:120px;">
          <div style="position: relative;left:30px;color:#98A0A7;display: inline-block;width: 100px;line-height: 120px">教材优惠</div>

          <el-input style="position: relative;left:20px;top:0px;display: inline-block;width: 100px" v-model='productOrderDiscount' type="number" min="0">

          </el-input>
          <div style="position: relative;left:30px;top:0px;display: inline-block;width: 100px"> 元</div>
        </div>


        <div style="width: 100%;position: relative;height: 1px;background-color: #D4DFE5">
        </div>

        <div  style="width: 100%;position: relative;top:20px;height:80px">
         <div style="position: relative;left:30px;color:#98A0A7;display: inline-block;width: 100px;">对内备注</div>

         <el-input style="position: relative;left:20px;display: inline-block;width: 40%" v-model='productmemoIn' placeholder='内部人员可见'>

         </el-input>

         <div style="position: relative;left:30px;color:#98A0A7;display: inline-block;width: 100px">对外备注</div>

         <el-input style="position: relative;left:20px;display: inline-block;width: 40%" v-model='productmemoOut' placeholder='订单打印可见'>

         </el-input>
       </div>

     </el-row>
     <el-row :span="6" style="margin-top:10px;height:40px"><span style='width:2px;height:100%;background-color:rgb(52, 178, 211) ;color: rgb(52, 178, 211);margin-right: 3px' >_</span>
      支付信息
    </el-row>
    <el-row style='background-color:white;width: 100%;position: relative;border-width: 1px;border-color: #D4DFE5;border-style: solid;margin-bottom:20px;margin-top: 0px;height:150px;margin-bottom: 10px'>

      <div style="width: 100%;position: relative;">
        <div style="position: relative;left:30px;top:20px;color:#98A0A7;display: inline-block;width: 100px">订单类型</div>
        <el-select v-model="orderType"  style='display: inline-block;margin-left:10px;position: relative;;left:10px;top:20px;width: 150px'>
          <el-option
          v-for="item in types"
          :label="item.name"
          :value="item.value"
          ></el-option>
        </el-select>
        <div style="position: relative;left:50px;top:20px;color:#98A0A7;display: inline-block;width: 100px">经办人</div>
               <!--  <el-input style="position: relative;left:10px;display: inline-block;width: 150px;top:20px" v-model='agent' :disabled='true'>
               </el-input> -->
               <el-select v-model="agent" filterable placeholder="搜索员工姓名"  @change="agentChange" style="position: relative;left:10px;display: inline-block;width: 150px;top:20px">
                <el-option
                v-for="item in staffList"
                :key="item._id"
                :label="item.name"
                :value="item._id"
                ></el-option>
              </el-select>


              <div style="position: relative;left:50px;top:20px;color:#98A0A7;display: inline-block;width: 100px">经办时间</div>
              <el-date-picker
              v-model="createdDate"
              type="date"
              placeholder="经办时间"
              style='position: relative;top:20px;left:30px'
              ></el-date-picker>

            </div>

            <div style="width: 100%;position: relative;margin-top: 20px">
              <div style="position: relative;left:30px;top:20px;color:#98A0A7;display: inline-block;width: 100px">付款方式</div>
              <el-select v-model="payType"  style='display: inline-block;margin-left:10px;position: relative;;left:10px;top:20px;width: 150px'>
                <el-option
                v-for="item in payTypes"
                :label="item.name"
                :value="item.value"
                ></el-option>
              </el-select>

              <div style="position: absolute;right:30px;font-size: 14px">
                <div style="display: inline-block;margin-right: 5px;color:#98A0A7">应收总计：</div>
                <div style="font-size:24px;display: inline-block;">{{parseFloat(courseTotal + productOrderTotal - productOrderDiscount).toFixed(2)}}</div>
                <div style="display: inline-block;margin-left: 10px;color:#98A0A7">元</div>
              </div>

            </div>

          </el-row>

          <el-button type="primary" @click="submit" style='margin-bottom: 100px;float:right;width: 150px;margin-right: 20px'>确认</el-button>
        </el-row>






        <el-dialog title="课程列表" :visible.sync="showCourse">

          <el-input placeholder="请输入课程名称" v-model="search.course" size="small">
            <el-button slot="append" icon="el-icon-search" @click="searchCourse"></el-button>
          </el-input>

          <el-table :data="courseList" height='500' :resizable='true' stripe>
            <el-table-column property="name" label="课程名称"></el-table-column>
            <el-table-column prop="pricePerClass" label="课程单价（元）"
            ></el-table-column>
            <el-table-column prop="pricePerHour" label="小时单价（元）" ></el-table-column>
            <el-table-column prop="pricePerMonth" label="包月单价（元）" ></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button @click="addCourse(scope.$index)" type="text" >添加</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="showCourse = false">确认</el-button>
          </div>
        </el-dialog>

        <el-dialog title="班级列表" :visible.sync="showClass">

          <el-input placeholder="请输入班级名称" v-model="search.class" size="small">
            <el-button slot="append" icon="el-icon-search" @click="searchClass"></el-button>
          </el-input>

          <el-table :data="classList" height='500' :resizable='true' stripe>
            <el-table-column prop="name" label="班级名" ></el-table-column>
            <el-table-column prop="course.name" label="课程名称"></el-table-column>
            <el-table-column prop="teacher.name" label="授课老师" ></el-table-column>
            <el-table-column prop="course.pricePerClass" label="课程单价（元）"
            ></el-table-column>
            <el-table-column prop="course.pricePerHour" label="小时单价（元）" ></el-table-column>
            <el-table-column prop="course.pricePerMonth" label="包月单价（元）" ></el-table-column>


            <el-table-column  label="操作">
              <template slot-scope="scope">
                <el-button @click="addClass(scope.$index)" type="text">添加</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="showClass = false">确认</el-button>
          </div>
        </el-dialog>


        <el-dialog title="商品列表" :visible.sync="showProduct">

          <el-input placeholder="请输入商品名称" v-model="search.product" size="small" style='width: 50%;display: inline-block;'>

          </el-input>

          <el-select v-model="search.productCategory" filterable placeholder="搜索商品分类" @change='categoryChange' size='small' style='margin-left:5%;width: 35%;display: inline-block;'>
            <el-option
            v-for="item in productCategoryList"
            :key="item._id"
            :label="item.name"
            :value="item._id"
            ></el-option>
          </el-select>
          <el-button  icon="el-icon-search" @click="searchProduct" style='display: inline-block;width: 10%' size='small' type='primary'></el-button>

          <el-table :data="productList" height='500'>
            <el-table-column property="name" label="商品名称"></el-table-column>
            <el-table-column property="typeText" label="类型"></el-table-column>
            <el-table-column prop="sellingPrice" label="单价"></el-table-column>
            <el-table-column prop="storage" label="库存"></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button v-if="scope.row.storage > 0" @click="addProduct(scope.$index)" type="text">添加</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="showProduct = false">确认</el-button>
          </div>
        </el-dialog>

        <el-dialog
        title="订单详情"
        :visible.sync="dialogVisible"
        width="1000px" height='300px'>

<!--         <div id="printMe" style="height:258px;width: 910.8px;border:solid 1px black" >
          <div style="padding-left:20px;padding-right: 20px;padding-top: 10px">

            <div style="font-size:14px;font-weight: 5px;display: inline-block;margin-right:50px"><span>校区:{{schoolObject != undefined ? schoolObject.name : ''}}</span></div>
            <div style="font-size:14px;font-weight: 5px;margin-top:5px;display: inline-block;margin-right:50px"><span>学生:{{student != undefined ?  studentList[studentIndex].name : ''}}</span></div>
            <div  style="font-size:14px;font-weight: 5px;margin-top:5px;display: inline-block;margin-right:50px">时间:{{processDate}}</div>
            <table style="margin-top: 5px;border: solid black 1px;width: 100%">
              <tr style="font-size: 12px">
                <th >课程</th>
                <th >班级</th>
                <th >购课方式</th>
                <th >数量</th>
                <th >有效日期</th>
                <th > 优惠（元）</th>
                <th > 小计（元）</th>
              </tr>
              <tr v-for='item in courseOrder' style="margin-bottom: 5px;font-size:12px">
                <td  style="text-align: center">{{item.course.name}}</td>
                <td  style="text-align: center">{{item.classDetail == undefined ? '' : item.classDetail.name}}</td>
                <td  style="text-align: center">{{item.type == 0 ? '课时' : (item.type == 1 ? '小时' : '包月')}}</td>
                <td  style="text-align: center">{{item.type == 0 ? item.numberOfClasses : (item.type == 1 ? item.numberOfHours : item.numberOfMonths)}}</td>
                <td  style="text-align: center">{{item.valid == 1 ?  item.validDate : '无'}}</td>
                <td style="text-align: center">{{item.discount}}</td>
                <td style="text-align: center">{{parseFloat(parseFloat(item.total) - parseFloat(item.discount)).toFixed(2)}}</td>
              </tr>
              <div style="height: 1px;background: black;width: 700%"></div>
              <tr style="border:solid 10px red;">
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td style="text-align: center">合计:{{parseFloat(courseTotal).toFixed(2)}}元</td>
              </tr>
            </table>

            <table style="margin-top: 5px;border: solid black 1px;width: 100%">
              <tr style="font-size: 12px">
                <th >商品名称</th>
                <th >数量</th>
                <th > 价格（元）</th>
              </tr>
              <tr v-for='item in productOrder' style="margin-bottom: 5px;font-size: 12px">
                <td  style="text-align: center">{{item.product.name}}</td>
                <td  style="text-align: center">{{item.amount}}</td>
                <td  style="text-align: center">{{parseFloat(item.product.sellingPrice * item.amount).toFixed(2)}}</td>
              </tr>
              <div style="height: 1px;background: black;width: 300%"></div>
              <tr style="border:solid 10px red;">
                <td></td>
                <td style="font-size: 12px;display: block;width: 100%;text-align: right">优惠 : {{productOrderDiscount}} 元</td>
                <td style="text-align: center">合计:{{-productOrderDiscount + productOrderTotal}}元</td>
              </tr>

            </table>



            <div style="font-size: 12px;display: block;width: 100%;text-align: right;margin-top: 2px"><b>总计 :{{parseFloat(courseTotal + productOrderTotal - productOrderDiscount).toFixed(2)}}元</b></div>
            <div  style="font-size:12px;font-weight: 5px;margin-top:2px;width: 50%; display: inline-block;white-space: nowrap;overflow: hidden;">课程备注:
              <text v-for="item in courseOrder" style="margin-right: 10px">
                {{item.memoOut}}
              </text>
            </div>

            <div  style="font-size:12px;font-weight: 5px;margin-top:2px;width: 50%;display: inline-block;">教材备注:<div>{{productmemoOut}}</div>
          </div>
        </div>
      </div> -->

      <div id="printMe" style="width: 910.8px;height:258px;border:solid 1px black;position: relative;">
                    <div>
                    <div style="font-size:24px;font-weight: 10px;text-align: center;border-bottom: solid 1px #cccccc;width: 100%;display: block">{{schoolObject != undefined ? schoolObject.name : ''}}收款单</div>
                    <div style="font-size:18px;font-weight: 5px;width: 33%;border-bottom: solid 1px #cccccc;display: inline-block;border-right:solid 1px #cccccc"><span>学生:{{student != undefined ?  studentList[studentIndex].name : ''}}</span></div>
                     <div style="font-size:18px;font-weight: 5px;width: 33%;border-bottom: solid 1px #cccccc;display: inline-block;border-right:solid 1px #cccccc;color: white"><span>_</span></div>
                    <div style="font-size:18px;font-weight: 5px;width: 33%;border-bottom: solid 1px #cccccc;display: inline-block;"><span>日期:{{processDate}}</span></div> 

                    <table style="width: 100%;border-collapse: collapse;display: block;font-size: 16px" >
                            <tr style="font-size: 18px;width: 100%;">
                                <th style="width: 36%;border-right:solid 1px black;border-bottom: solid 1px black;padding:0">项目</th>
                                <th style="width: 11%;border-right:solid 1px black;border-bottom: solid 1px black">数量</th>
                                <th style="width: 11%;border-right:solid 1px black;border-bottom: solid 1px black">单位</th>
                                <th style="width: 11%;border-right:solid 1px black;border-bottom: solid 1px black">单价(元)</th>
                                <th style="width: 11%;border-right:solid 1px black;border-bottom: solid 1px black">合计金额(元)</th>
                                <th style="width: 11%;border-right:solid 1px black;border-bottom: solid 1px black">优惠金额(元)</th>
                                <th style="width: 11%;border-right:solid 1px black;border-bottom: solid 1px black">实收金额(元)</th>
                            </tr>
                             <tr v-for='item in courseOrder' style="margin-bottom: 5px;font-size:12px">
                                <td  style="text-align: center;border-right:solid 1px black;border-bottom: solid 1px black;">{{item.course.name}}</td>
                                <td  style="text-align: center;border-right:solid 1px black;border-bottom: solid 1px black;">{{item.amount}}</td>
                                <td  style="text-align: center;border-right:solid 1px black;border-bottom: solid 1px black;">{{item.type == 0 ? '课时' : (item.type == 1 ? '小时' : '包月')}}</td>
                                <td style="text-align: center;border-right:solid 1px black;border-bottom: solid 1px black;">{{item.unitPrice}}</td>
                                <td style="text-align: center;border-right:solid 1px black;border-bottom: solid 1px black;">{{parseFloat(item.total)}}</td>
                                <td style="text-align: center;border-right:solid 1px black;border-bottom: solid 1px black;">{{item.discount}}</td>
                                <td style="text-align: center;border-right:solid 1px black;border-bottom: solid 1px black;">{{parseFloat(parseFloat(item.total) - parseFloat(item.discount)).toFixed(2)}}</td>
                                <!-- <div style="height: 1px;background: black;width: 1200%"></div> -->
                              </tr>

                            <tr v-for='(item, index) in productOrder' style="margin-bottom: 3px;font-size: 12px">
                                <td style="text-align: center;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;border-right:solid 1px black;border-bottom: solid 1px black">{{item.product.name}}</td>
                                <td style="text-align: center;border-right:solid 1px black;border-bottom: solid 1px black;">{{item.amount}}</td>
                                <td style="text-align: center;border-right:solid 1px black;border-bottom: solid 1px black;color:white">-</td>
                                <td style="text-align: center;border-right:solid 1px black;border-bottom: solid 1px black;">{{item.product.sellingPrice}}</td>
                                <td style="text-align: center;border-right:solid 1px black;border-bottom: solid 1px black;" v-if='item.amount != undefined'>{{parseFloat(item.product.sellingPrice * item.amount).toFixed(2)}}</td>
                                <td style="text-align: center;border-right:solid 1px black;border-bottom: solid 1px black;" v-if='item.amount != undefined'>{{index == 0 ? productOrderDiscount : 0}}</td>
                                <td style="text-align: center;border-right:solid 1px black;border-bottom: solid 1px black;" v-if='item.amount != undefined'>{{parseFloat(item.product.sellingPrice * item.amount - (index == 0 ? productOrderDiscount : 0)).toFixed(2)}}</td>
                                <td style="text-align: center;border-right:solid 1px black;border-bottom: solid 1px black;color:white" v-if='item.amount == undefined'>-</td>
                                <td style="text-align: center;border-right:solid 1px black;border-bottom: solid 1px black;color:white" v-if='item.amount == undefined'>-</td>
                                <td style="text-align: center;border-right:solid 1px black;border-bottom: solid 1px black;color:white" v-if='item.amount == undefined'>-</td>
                            </tr>
                            
                            
                        </table>
                        <div  style="font-size:16px;font-weight: 5px;width: 100%;text-align: left;border-bottom: solid 1px black;">合计:总计 :{{parseFloat(courseTotal + productOrderTotal - productOrderDiscount).toFixed(2)}}元</div>
                       

                        <div  style="font-size:16px;font-weight: 5px;width: 100%;text-align: left;border-bottom: solid 1px black;display: inline-block;">支付方式:{{payTypeText}}</div>
                         <div  style="font-size:16px;font-weight: 5px;width: 100%;text-align: left;border-bottom: solid 1px black;text-overflow: ellipsis;white-space: nowrap;overflow: hidden;">备注:{{productmemoOut}}</div>

                         <div  style="font-size:16px;font-weight: 5px;width: 33%;text-align: left;display: inline-block;">收款单位(章):</div>
                         <div  style="font-size:16px;font-weight: 5px;width: 33%;text-align: left;display: inline-block;">经办人:{{agentName}}</div>
                         <div  style="font-size:16px;font-weight: 5px;width: 33%;text-align: left;display: inline-block;">缴款人签字:</div>
                     </div>
            </div>



      <span slot="footer" class="dialog-footer">
        <el-button @click='printabc("div")'>打印订单</el-button>
        <el-button type="primary" @click="closePrint">关闭</el-button>
      </span>
    </el-dialog>

    <!-- </block> -->
  </el-form>

</d2-container>
</template>

<script>
  import request from "@/plugin/axios";
  import {mapState} from "vuex";
  import formatTime from "@/libs/util.formatTime"

  export default {
    data() {
      return {
        studentList: [],
        studentIndex : -1,
        parentInfo : '',
        courseCount : 0,
        total: 0,
        staffList : [],
        pageSize: 10,
        currentPage: 1,
        classroom: [],
        disabled : false,
        agent: '',
        dataSearch: {},
        student : undefined,
        courseList : [],
        courseOrderClassList : [],
        classList : [],
        courseOrder : [],
        productOrderDiscount : 0,
        productList : [],
        courseTotal : 0,
        productOrder : [],
        showProduct : false,
        loading : false,
        productOrderTotal : 0,
        initClass : undefined,
        initCourse : undefined,
        productmemoIn:'',
        createdDate : new Date(),
        payType : 0,
        productCategoryList :[],
        productmemoOut:'',
        dialogVisible :false,
        orderType : 0,
        types : [{value:0,name:'新生'},{value:1,name:'试听'},{value:2,name:'转介绍'},{value:3,name:'续报'},{value:4,name:'接待'},{value:5,name:'教材'}],
        payTypes : [{value:0,name:'现金'},{value:1,name:'银行卡'},{value:3,name:'微信'},{value:2,name:'支付宝'}],
        search :{
          course : undefined,
          product : undefined,
          agent : undefined,
          productCategory :undefined
        },
        showCourse : false,
        showClass : false,
        avClass : [],
        avClassText : '',
        agentName : ''
      };
    },
    computed: {
      ...mapState('d2admin/user', ['info']),
    },
    methods: {

      studentChange(){
        this.parentInfo = ''
        this.courseCount = 0
        let index = this.studentList.map(function(e){return e._id}).indexOf(this.student)
        this.studentIndex = index
                  // get student parents & courses
                  if (this.student == 'null'){
                    this.disabled = true
                    return false
                  }
                  else{
                    this.disabled = false
                  }
                  request({
                    url: "/parent/getParentsByStudent",
                    method: "post",
                    data : {student:this.student}
                  }).then(res => {
                    let temp = ''
                    for (var i = 0 ; i < res.data.length ; i++){
                      temp += res.data[i].cellphone + ' (' + res.data[i].name + ' ' + res.data[i].relation + ')'
                      if (i != res.data.length - 1){
                        temp += ', '
                      }
                    }
                    this.parentInfo = temp
                    // 返回数据
                  });

                  request({
                    url: "/courseorder/getStudentCourseAbstract",
                    method: "post",
                    data : {student:this.student,classStatus:0}
                  }).then(res => {
                    this.courseCount = res.data.total
                    this.studentCourses = res.data.courseOrder
                    this.avClass = res.data.courseOrder.map(e=>{return e.class == null ? '' : e.class})
                    this.avClass = this.avClass.filter(e=>{return e != ''})
                    this.avClassText = this.avClass.map(e=>{return e.name}).join('  ')
                    console.log(this.avClass,'sss')
                    // 返回数据
                    console.log(this.studentCourses,'sss')
                    if ((this.initCourse != undefined) && (this.initClass == undefined)){
                      this.searchCourse()
                    }
                    if (this.initClass != undefined){
                      this.initCourse = undefined
                      this.searchClass()
                    }

                  });


                },

                agentChange(){
                  let index = this.staffList.map(e=>{return e._id}).indexOf(this.agent)
                  if (index > -1){
                    this.agentName = this.staffList[index].name
                  }
                },

                getStudentList(page, pageSize) {
                  let data = {};
                  (data.page = page), (data.pageSize = pageSize);
                  data.school = this.school
                  request({
                    url: "/student/getStudentList",
                    method: "post",
                    data : data
                  }).then(res => {
                    this.studentList = [{name:'校外学生',_id:'null'}]
                    this.studentList = this.studentList.concat(res.data.students);
                    if (this.studentID != undefined){
                      let index = this.studentList.map(function(e){return e._id}).indexOf(this.studentID)
                      this.student = this.studentList[index]._id
                      this.studentIndex = index
                      console.log(index,'index')
                      this.studentChange()

                    }
                    // 返回数据
                  });
                },

                newStudent(){
                  this.$router.push({ path: "/student-new?from=mainorder" })
                },
                searchCourse(){
                  let data = {
                    page: 1,
                    pageSize: 99999999,
                    name: this.search.course,
                    school: [this.school]
                  }

                  request({
                    url: "/course/getCourseList",
                    method: "post",
                    data
                  }).then(res => {
                    if (res.code == 200) {
                      this.courseList = res.data.course
                      console.log(this.initCourse,'dsdds')
                      if (this.initCourse != undefined){

                        let temp = {}
                        let index = this.courseList.map(function(e){return e._id}).indexOf(this.initCourse)
                        temp.courseID = this.initCourse
                        temp.course = this.courseList[index]
                        temp.total = 0.0
                        temp.memoIn = ''
                        temp.memoOut = ''
                        temp.type = 0
                        temp.numberOfClasses = 0
                        temp.numberOfHours = 0
                        temp.numberOfMonths = 0
                        temp.amount = 0 
                        temp.validDate = null
                        temp.bonusType = 0
                        temp.valid = false
                        temp.startDate = null
                        temp.payWay = 0
                        temp.discount = 0.0
                        temp.class = ''
                        temp.pays = [{type:0,name:'1课时=' + temp.course.pricePerClass + '元',price:temp.course.pricePerClass},{type:1,name:'1小时=' + temp.course.pricePerHour+ '元',price:temp.course.pricePerHour},{type:2,name:'1个月=' + temp.course.pricePerMonth+ '元',price:temp.course.pricePerMonth}]

                        let index3 = this.studentCourses.map(function(e){return e.course._id}).indexOf(this.initCourse)

                        if (index3 != -1){
                          temp.continue = true
                          temp.type = this.studentCourses[index3].type
                        }
                        this.initCourse = undefined
                        this.courseOrder.push(temp)

                      }
                    }
                  });
                },


                searchClass(){
                  request({
                    url: "/class/getClassList",
                    method: "post",
                    data: {
                      page: 1,
                      pageSize: 99999999,
                      name: this.search.class,
                      school: [this.school]
                    }
                  }).then(res => {
                    if (res.code == 200) {
                      this.classList = res.data.class

                      if (this.initClass != undefined){
                        let temp = {}
                        let index = this.classList.map(function(e){return e._id}).indexOf(this.initClass)
                        if (index == -1){
                          return false
                        }
                        temp.courseID = this.classList[index].course._id
                        temp.course =  this.classList[index].course
                        temp.course.school = this.classList[index].school
                        temp.total = 0.0
                        temp.memoIn = ''
                        temp.memoOut = ''
                        temp.type = 0
                        temp.numberOfClasses = 0
                        temp.numberOfHours = 0
                        temp.numberOfMonths = 0
                        temp.amount = 0
                        temp.bonusType = 0
                        temp.validDate = null
                        temp.valid = false
                        temp.payWay = 0
                        temp.startDate = null
                        temp.discount = 0.0
                        temp.class = this.classList[index]._id
                        temp.pays = [{type:0,name:'1课时=' + temp.course.pricePerClass + '元',price:temp.course.pricePerClass},{type:1,name:'1小时=' + temp.course.pricePerHour+ '元',price:temp.course.pricePerHour},{type:2,name:'1个月=' + temp.course.pricePerMonth+ '元',price:temp.course.pricePerMonth}]
                        let data = {}
                        data._id = this.classList[index]._id
                        data.name = this.classList[index].name
                        data.teacher = this.classList[index].teacher == null ? '暂无' : this.classList[index].teacher.name
                        data.startDateText = formatTime.formatDate(this.classList[index].startDate)
                        data.studentLimit = this.classList[index].studentLimit
                        data.students = this.classList[index].student.length
                        temp.courseOrderClassList = [data]
                        let index2 = this.courseOrder.map(function(e){return e.courseID}).indexOf(temp.courseID)
                        if (index2 == -1){
                          let index3 = this.studentCourses.map(function(e){return e.course._id}).indexOf(temp.courseID)
                          if (index3 != -1){
                            temp.continue = true
                            temp.type = this.studentCourses[index3].type
                            temp.class = this.studentCourses[index3].class
                          }
                          this.courseOrder.push(temp)
                        }
                        this.initClass = undefined
                      }

                    }
                  });
                },

                classClick(index){
                  request({
                    url: "/class/getClassList",
                    method: "post",
                    data: {
                      page: 1,
                      pageSize: 99999999,
                        // name: this.search.class,
                        school: [this.school],
                        course: this.courseOrder[index].course._id
                      }
                    }).then(res => {
                      if (res.code == 200) {

                        console.log(this.courseOrder[index].courseOrderClassList)
                        let temp = []
                        for (var i = 0 ;i < res.data.class.length ;i++) {
                          let data = {}
                          data._id = res.data.class[i]._id
                          data.name = res.data.class[i].name
                          data.teacher = res.data.class[i].teacher == null ? '暂无' : res.data.class[i].teacher.name
                          data.startDateText = formatTime.formatDate(res.data.class[i].startDate)
                          data.studentLimit = res.data.class[i].studentLimit
                          data.students = res.data.class[i].student.length
                          temp.push(data)
                        }
                        this.courseOrder[index].courseOrderClassList = temp
                        this.courseOrder.push({})
                        this.courseOrder.splice(this.courseOrder.length-1, 1)
                      }
                    });
                  },

                  classChange(index){
                    let index2 = this.courseOrder[index].courseOrderClassList.map(function(e){return e._id}).indexOf(this.courseOrder[index].class)
                    if (this.courseOrder[index].courseOrderClassList[index2].studentLimit <= this.courseOrder[index].courseOrderClassList[index2].students){
                     this.$alert("", "该班级已满员，请选择其他班级", {
                      confirmButtonText: "确定"
                    });
                     this.courseOrder[index].class = ''
                   }
                 },


                 addCourse(index){
                  if (!this.student){
                    this.$alert('','请先选择学生',{
                      confirmButtonText :'确定'
                    })
                    return false
                  }
                  let temp = {}
                  temp.courseID = this.courseList[index]._id
                  temp.course = this.courseList[index]
                  temp.total = 0.0
                  temp.memoIn = ''
                  temp.memoOut = ''
                  temp.type = 0
                  temp.numberOfClasses = 0
                  temp.numberOfHours = 0
                  temp.numberOfMonths = 0
                  temp.amount = 0 
                  temp.validDate = null
                  temp.bonusType = 0
                  temp.valid = false
                  temp.startDate = null
                  temp.payWay = 0
                  temp.discount = 0.0
                  temp.class = ''
                  temp.pays = [{type:0,name:'1课时=' + temp.course.pricePerClass + '元',price:temp.course.pricePerClass},{type:1,name:'1小时=' + temp.course.pricePerHour+ '元',price:temp.course.pricePerHour},{type:2,name:'1个月=' + temp.course.pricePerMonth+ '元',price:temp.course.pricePerMonth}]

                  let index2 = this.courseOrder.map(function(e){return e.courseID}).indexOf(temp.courseID)
                  if (index2 == -1){

                    let index3 = this.studentCourses.map(function(e){return e.course._id}).indexOf(temp.courseID)
                    if (index3 != -1){
                      temp.continue = true
                      temp.type = this.studentCourses[index3].type
                    }
                    this.courseOrder.push(temp)
                  }

                },
                addClass(index){
                  if (!this.student){
                    this.$alert('','请先选择学生',{
                      confirmButtonText :'确定'
                    })
                    return false
                  }
                  console.log(this.classList[index].name,'dssd')
                  let temp = {}
                  temp.courseID = this.classList[index].course._id
                  temp.course =  this.classList[index].course
                  temp.course.school = this.classList[index].school
                  temp.total = 0.0
                  temp.memoIn = ''
                  temp.memoOut = ''
                  temp.type = 0
                  temp.numberOfClasses = 0
                  temp.numberOfHours = 0
                  temp.numberOfMonths = 0
                  temp.amount = 0
                  temp.bonusType = 0
                  temp.validDate = null
                  temp.valid = false
                  temp.payWay = 0
                  temp.startDate = null
                  temp.discount = 0.0
                  temp.class = this.classList[index]._id
                  temp.classDetail = this.classList[index]
                  temp.pays = [{type:0,name:'1课时=' + temp.course.pricePerClass + '元',price:temp.course.pricePerClass},{type:1,name:'1小时=' + temp.course.pricePerHour+ '元',price:temp.course.pricePerHour},{type:2,name:'1个月=' + temp.course.pricePerMonth+ '元',price:temp.course.pricePerMonth}]

                // let data = {}
                // data._id = this.classList[index]._id
                // data.name = this.classList[index].name
                // data.teacher = this.classList[index].teacher == null ? '暂无' : this.classList[index].teacher.name
                // data.startDateText = formatTime.formatDate(this.classList[index].startDate)
                // data.studentLimit = this.classList[index].studentLimit
                // data.students = this.classList[index].student.length
                // temp.courseOrderClassList = [data]
                let index2 = this.courseOrder.map(function(e){return e.courseID}).indexOf(temp.courseID)
                if (index2 == -1){
                  let index3 = this.studentCourses.map(function(e){return e.course._id}).indexOf(temp.courseID)
                  if (index3 != -1){
                    temp.continue = true
                    temp.type = this.studentCourses[index3].type
                    temp.class = this.studentCourses[index3].class._id
                    temp.classDetail = this.studentCourses[index3].class
                  }
                  this.courseOrder.push(temp)
                  this.classClick(this.courseOrder.length - 1)
                }

                
                
              },

              orderTypeChange(index){
                if (this.courseOrder[index].type == 0){

                  this.courseOrder[index].total = parseFloat(this.courseOrder[index].amount * parseFloat(this.courseOrder[index].course.pricePerClass)).toFixed(2)
                }
                else if (this.courseOrder[index].type == 1){
                  this.courseOrder[index].total = parseFloat(this.courseOrder[index].amount * parseFloat(this.courseOrder[index].course.pricePerHour)).toFixed(2)
                }
                else if (this.courseOrder[index].type == 2){
                  this.courseOrder[index].total = parseFloat(this.courseOrder[index].amount * parseFloat(this.courseOrder[index].course.pricePerMonth)).toFixed(2)
                }


                let total = 0
                for (var i = 0 ; i < this.courseOrder.length ; i++){
                  total += this.courseOrder[i].total - this.courseOrder[i].discount
                }

                this.courseTotal = total
              },

              orderDiscountChange(index){
                let total = 0
                for (var i = 0 ; i < this.courseOrder.length ; i++){
                  total += this.courseOrder[i].total - this.courseOrder[i].discount
                }
                this.courseTotal = total
              },


              searchProduct(page) {
                let data = {
                  page: page,
                  pageSize: 99999999,
                  name: this.search.product,
                  school: [this.school]
                }
                if (this.search.productCategory){
                  data.category = this.search.productCategory
                }
                request({
                  url: "/product/getProductList",
                  method: "post",
                  data: data
                }).then(res => {
                  if (res.code == 200) {
                    res.data.product.forEach(e=>{
                      e.typeText = e.productCategory.map(a=>{return a.name}).join(',')
                    })
                    this.productList = res.data.product
                        // this.searchCount = res.data.count
                      }
                    });
              },


              amountChange(index){
                if (this.courseOrder[index].type == 0){

                  this.courseOrder[index].total = parseFloat(this.courseOrder[index].amount * parseFloat(this.courseOrder[index].course.pricePerClass)).toFixed(2)
                }
                else if (this.courseOrder[index].type == 1){
                  this.courseOrder[index].total = parseFloat(this.courseOrder[index].amount * parseFloat(this.courseOrder[index].course.pricePerHour)).toFixed(2)
                }
                else if (this.courseOrder[index].type == 2){
                  this.courseOrder[index].total = parseFloat(this.courseOrder[index].amount * parseFloat(this.courseOrder[index].course.pricePerMonth)).toFixed(2)
                }

                let total = 0
                for (var i = 0 ; i < this.courseOrder.length ; i++){
                  total += this.courseOrder[i].total - this.courseOrder[i].discount
                }
                this.courseTotal = total
              },

              productAmountChange(index){
                let prices = this.productOrder.map(function(e){return e.product.sellingPrice * e.amount})

                let total = 0
                for (var i = 0 ; i < prices.length ; i++){
                  total += prices[i]
                }
                this.productOrderTotal = total
              },

              addProduct(index){
                if (!this.student){
                  this.$alert('','请先选择学生',{
                    confirmButtonText :'确定'
                  })
                  return false
                }
                let temp = {}
                temp.product = this.productList[index]
                temp.amount = 1
                temp.picked = false
                
                
                this.productOrder.push(temp)
                let prices = this.productOrder.map(function(e){return e.product.sellingPrice * e.amount})

                let total = 0
                for (var i = 0 ; i < prices.length ; i++){
                  total += prices[i]
                }
                this.productOrderTotal = total

              },

              removeProductOrder(){
                this.productOrder = []
                this.productOrderTotal = 0
                this.productOrderDiscount = 0
              },

              removeProductOrderDetail(index){
                this.productOrder.splice(index,1)
                
                let prices = this.productOrder.map(function(e){return e.product.sellingPrice * e.amount})

                let total = 0
                for (var i = 0 ; i < prices.length ; i++){
                  total += prices[i]
                }
                this.productOrderTotal = total
              },

              monthChange(){
                console.log(this.courseOrder[0].startDate)
              },


              getStaffList(page,pageSize){
                let data = {};
                (data.page = page), (data.pageSize = pageSize);
                data.school = this.school
                request({
                  url: "/staff/getStaffList",
                  method: "post",
                  data : data
                }).then(res => {
                  this.staffList = res.data.staff;

                    // 返回数据
                  });
              },

              submit(){
                let subtotal = 0
                let i = 0
                while (i < this.courseOrder.length){
                  subtotal += this.courseOrder[i].total - this.courseOrder[i].discount

                  i = i + 1


                }
                subtotal += this.productOrderTotal - this.productOrderDiscount
                if (subtotal <=0){

                  this.$confirm('是否继续？','当前订单金额小于等于0',{
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                  }).then((value) => {
                    this.confirmOrder()
                           // return false
                         })
                  .catch(()=>{
                      // this.getCourseOrderOne(this.id)
                    })
                }
                else{
                  this.confirmOrder()
                }
              },

              confirmOrder(){
                this.loading = false
                // check time date valid
                if ((this.productOrder.length > 0 ) || (this.courseOrder.length > 0)){
                    // check info complete
                    var i = 0;
                    while (i < this.courseOrder.length){
                      if (this.courseOrder[i].total - this.courseOrder[i].discount < 0){
                        i = this.courseOrder.length
                        this.$alert('','有课程收费不正确，请再次确认！', {
                          confirmButtonText: "确定"
                        })
                        this.loading = false
                        return false
                      }

                      if (this.courseOrder[i].type == 2){

                        if ((this.courseOrder[i].startDate == undefined) || (new Date(this.courseOrder[i].startDate) == 'Invalid Date')){
                          i = this.courseOrder.length
                         this.$alert('','月费学生开始时间不能为空，请再次确认！', {
                         confirmButtonText: "确定"
                       })
                     console.log('hhh')
                        this.loading = false
                        return false
                        
                        }
                        
                      }

                      i = i + 1
                    }
                    i = 0 
                    while (i < this.productOrder.length){
                      if (this.productOrder[i].amount == 0){
                        i = this.productOrder.length
                        this.$alert('','有教材数量为0，请再次确认！', {
                          confirmButtonText: "确定"
                        })
                        this.loading = false
                        return false
                      }
                      else{
                        i = i + 1
                      }

                    }
                    if (this.student == undefined){
                      this.$alert('','请选择学生', {
                        confirmButtonText: "确定"
                      })
                      this.loading = false
                      return false
                    }

                    let data = {}
                    data.courseOrder = this.courseOrder
                    data.productOrder = this.productOrder
                    data.productOrderDiscount = this.productOrderDiscount
                    data.student = this.student
                    data.agent = this.agent
                    data.school = this.school
                    data.createdDate = this.createdDate
                    data.payWay = this.payType
                    data.orderType = this.orderType
                    data.productmemoOut = this.productmemoOut
                    data.productmemoIn = this.productmemoIn
                    data.productOrderTotal = this.productOrderTotal
                    request({
                      url: "/courseOrder/addMainOrder",
                      method: "post",
                      data: data
                    }).then(res => {
                      this.loading = false
                      if (res.code == 200) {

                        if (res.error == undefined){

                          this.$alert('','订单创建成功！',{
                            confirmButtonText :'确定'
                          }).then(resx=>{
                            if (this.studentID != undefined){
                              this.courseOrder = []
                              this.productOrder = []
                              this.student = undefined
                              this.productOrderDiscount = 0
                              this.productOrderTotal = 0
                              this.studentIndex = -1
                              this.parentInfo = ''
                              this.productmemoOut = ''
                              this.productmemoIn = ''
                              this.orderType = 0
                              this.payType = 0
                              this.courseTotal = 0
                              this.productList = []
                              this.classList = []
                              this.refresh = true
                              this.$router.back(-1)
                            }
                            else{
                              this.$confirm('','是否打印订单？',{
                                confirmButtonText : '打印',
                                cancelButtonText : '取消'
                              }).then(resx=>{

                                this.processDate = formatTime.formatTime(new Date())
                                this.payTypeText = ''
                                if (this.payType == 0){
                                  this.payTypeText = '现金'
                                }
                                else if (this.payType == 1){
                                  this.payTypeText = '银行卡'
                                }
                                else if (this.payType == 2){
                                  this.payTypeText = '支付宝'
                                }
                                else if (this.payType ==3){
                                  this.payTypeText = '微信'
                                }
                                else if (this.payType == 4){
                                  this.payTypeText = '其他'
                                }

                                for (var i = 0 ;i < this.courseOrder.length ; i++){
                                  this.courseOrder.unitPrice = 0
                                  if (this.courseOrder[i].type == 0){
                                    this.courseOrder[i].unitPrice = this.courseOrder[i].course.pricePerClass
                                  }
                                  else if (this.courseOrder[i].type == 1){
                                    this.courseOrder[i].unitPrice = this.courseOrder[i].course.pricePerHour
                                  }
                                  else if (this.courseOrder[i].type == 2){
                                    this.courseOrder[i].unitPrice = this.courseOrder[i].course.pricePerMonth
                                  }
                                }

                                this.dialogVisible = true
                              }).catch(value=>{
                                this.disabled = false
                                this.courseOrder = []
                                this.productOrder = []
                                this.student = undefined
                                this.productOrderDiscount = 0
                                this.productOrderTotal = 0
                                this.studentIndex = -1
                                this.parentInfo = ''
                                this.productmemoOut = ''
                                this.productmemoIn = ''
                                this.orderType = 0
                                this.payType = 0
                                this.courseTotal = 0
                                this.productList = []
                                this.classList = []
                              })
                            }

                                // show dialog and print 
                                // else if (res.data.courseID != ''){
                                //   this.$router.push({path: '/course-order-list?id=' + res.data.courseID})
                                // }
                                // else if (res.data.productID != ''){
                                //   this.$router.push({path: '/product-order-list?id=' + res.data.productID})
                                // }
                              })
                        }
                        else{
                          this.$alert('',res.error,{
                            confirmButtonText :'确定'
                          })
                        }
                      }
                    });

                  }

                },
                closePrint(){
                  this.disabled = false
                  this.courseOrder = []
                  this.productOrder = []
                  this.student = undefined
                  this.productOrderDiscount = 0
                  this.productOrderTotal = 0
                  this.studentIndex = -1
                  this.parentInfo = ''
                  this.productmemoOut = ''
                  this.productmemoIn = ''
                  this.orderType = 0
                  this.payType = 0
                  this.courseTotal = 0
                  this.productList = []
                  this.classList = []
                  this.dialogVisible = false
                },
                printabc(div){
                  var PW =  window.open('', '', 'left=0,top=0,width=600,height=848.6,toolbar=0,scrollbars=0,status=0');
            // 获取div中的html内容
            PW.document.write(document.getElementById('printMe').innerHTML);
            PW.document.close();
            PW.focus();
            PW.print();
            PW.close();

            this.courseOrder = []
            this.productOrder = []
            this.student = undefined
            this.productOrderDiscount = 0
            this.productOrderTotal = 0
            this.studentIndex = -1
            this.parentInfo = ''
            this.productmemoOut = ''
            this.productmemoIn = ''
            this.orderType = 0
            this.payType = 0
            this.courseTotal = 0
            this.productList = []
            this.classList = []
            this.disabled = false

            return false;

          },

          getCategoryList() {
            request({
              url: "/productCategory/getProductCategoryList",
              method: "post",
              data: {school:this.school,pageSize:99999}
            }).then(res => {
              this.productCategoryList = [{_id:undefined,name:'全部'}]
              this.productCategoryList = this.productCategoryList.concat(res.data.productCategory);
            });
          },

          removeCourseOrder(index){
            if (this.courseOrder._id == undefined){
              this.courseOrder.splice(index,1)
            }
            else{

            }
          },
          load(){
            this.school = this.info.school[0]._id
            this.schoolObject = this.info.school[0]
            this.agent = this.info.staff == undefined ? null : this.info.staff._id
            this.staffname = this.info.staff == undefined ? '' : this.info.staff.name
            this.agentName = this.staffname
            if (this.agent == null){
              this.$alert('请先绑定员工，否则无法成功创建订单','该账号尚未绑定员工')
            }
            this.getStudentList(1,10000)
            this.getStaffList(1,10000)
            this.getCategoryList()
            if (this.$route.query.student != undefined) {
              this.studentID = this.$route.query.student

            }
            if (this.$route.query.class != undefined){
              this.initClass = this.$route.query.class
            }
            if (this.$route.query.course != undefined){
              this.initCourse = this.$route.query.course
            }
          }

        },
        beforeRouteLeave(to,from,next){
          if (this.refresh){
            to.meta.refresh = 'a'
            this.refresh = undefined
          }
          next()
        },
        beforeRouteEnter(to,from,next){
          console.log(to.meta.refresh == 'a','tee',to.meta.refresh)
          if (to.meta.refresh == 'a'){
            to.meta.refresh = undefined
            next(v=>{
              v.load()
              v.studentId = to.meta.studentId
              to.meta.studentId = undefined
            })
          }
          else{
            next()
          }

        },
        created() {
            // this.dataSearch.page = this.currentPage;
            // this.dataSearch.pageSize = this.pageSize;

            this.load()


          }
        };
      </script>
